package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.fragments.fragmentsview.HomeFragment;
import com.example.myapplication.fragments.fragmentsview.MeFragment;
import com.example.myapplication.fragments.fragmentsview.SearchFragment;
import com.example.myapplication.fragments.fragmentsview.WarningFragment;
import com.example.myapplication.loginandregister.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private List<Fragment> fragments = new ArrayList<>();
    private int position = 0;
    private Fragment tempFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        initFragment();
        initRadioButton();
        setListener();
    }

    //设置RadioButton的大小
    private void initRadioButton() {
        Drawable homepageDrawable = getResources().getDrawable(R.drawable.home_drawable_selector);
        homepageDrawable.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        binding.radio1.setCompoundDrawables(null, homepageDrawable, null, null);//只放上面

        Drawable homepageDrawable2 = getResources().getDrawable(R.drawable.warning_drawable_selector);
        homepageDrawable2.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        binding.radio2.setCompoundDrawables(null, homepageDrawable2, null, null);//只放上面

        Drawable homepageDrawable5 = getResources().getDrawable(R.drawable.center_icon);
        homepageDrawable5.setBounds(0, -10, 180, 135);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        binding.center.setCompoundDrawables(null, homepageDrawable5, null, null);//只放上面

        Drawable homepageDrawable3 = getResources().getDrawable(R.drawable.search_drawable_selector);
        homepageDrawable3.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        binding.radio3.setCompoundDrawables(null, homepageDrawable3, null, null);//只放上面

        Drawable homepageDrawable4 = getResources().getDrawable(R.drawable.me_drawable_selector);
        homepageDrawable4.setBounds(0, 0, 69, 69);//参数1是距左右边距离，参数2是距上下边距离，参数3长度,参数4宽度
        binding.radio4.setCompoundDrawables(null, homepageDrawable4, null, null);//只放上面
    }

    //初始化fragments
    private void initFragment() {
        HomeFragment homeFragment = new HomeFragment();
        WarningFragment warningFragment = new WarningFragment();
        SearchFragment searchFragment = new SearchFragment();
        MeFragment meFragment = new MeFragment();
        fragments.add(homeFragment);
        fragments.add(warningFragment);
        fragments.add(searchFragment);
        fragments.add(meFragment);
    }

    //RadioGroup的事件监听
    private void setListener() {
        binding.mainRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio1:
                        position = 0;
                        break;
                    case R.id.radio2:
                        position = 1;
                        break;
                    case R.id.center:
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radio3:
                        position = 2;
                        break;
                    case R.id.radio4:
                        position = 3;
                        break;
                    default:
                        position = 0;
                        break;
                }
                switchFragment(tempFragment,fragments.get(position));
            }
        });
        binding.mainRgp.check(R.id.radio1);
    }

    private void switchFragment(Fragment fromFragment,Fragment nextFragment){
        if(tempFragment!=nextFragment){
            tempFragment=nextFragment;
            if(nextFragment!=null){
                FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                if(!nextFragment.isAdded()){
                    if(fromFragment!=null){
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.frameLayout,nextFragment).commit();
                }else {
                    if(fromFragment!=null){
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }

}